<?php

require_once "lib/Global.php";
require_once "const.inc";
require_once "modules/forum/const.inc";

class Helper_Forum extends Zend_Controller_Action_Helper_Abstract
{   
  private function getBookmarkTopics($type, $refid, $limit, $must_count){
    switch ($type){
      case FORUM_GROUP_TYPE:
        $key = "forum_group_bookmark_$refid";
        break;
      case FORUM_EVENT_TYPE:
        $key = "forum_event_bookmark_$refid";
        break;
      case FORUM_NETWORK_TYPE:
        $key = "forum_network_bookmark_$refid";
        break;
      default:
        return;
    }
    $ctrl = $this->getActionController();
    $data = $ctrl->getCache($key,'helper_');
    
    $data_changed = false;
        
    if (!$data || !is_array($data)){
      $data = array();
    }
    
    if (!isset($data['topics'])){
      $count = $must_count && (!isset($data['count']));
      $t_model = AppGlobal::loadModel('Topics','forum');
      $result = $t_model->getRefTopics($type, $refid, 1, $limit, $must_count);
      if (isset($result)){
        $data['topics'] = $result['data'];
        if ($count)
          $data['count'] = $result['record_count'];
        
        $data_changed = true;
      }     
    }
    
    if ($data_changed)
      $ctrl->setCache($key,$data,'helper_');
    
    return $data;
    
  }
  
  public function getGroupBookmarkTopics($gid, $limit, $must_count = true){
    return $this->getBookmarkTopics(FORUM_GROUP_TYPE, $gid, $limit, $must_count);
  }
  
  public function getEventBookmarkTopics($gid, $limit, $must_count = true){
    return $this->getBookmarkTopics(FORUM_EVENT_TYPE, $gid, $limit, $must_count);
  }
  
  public function getNetworkBookmarkTopics($gid, $limit, $must_count = true){
    return $this->getBookmarkTopics(FORUM_NETWORK_TYPE, $gid, $limit, $must_count);
  }
  
}


